<?php
// include function files for this application
require_once('/home/domains/uny-con.com/public_html/uny-con_fns.php');
if(!session_id()) { session_start(); }

//get some variables:

//This is an admin only script so first lets check to make sure the user is valid and an admin. then we can do toher stuff.

if (!check_valid_user() || ($_SESSION['admin'] != 't')) {
	do_html_header('Unauthorized access', staysmall);
	echo 'You are not logged in or are not authorized to view this page. Please move along.';
	print_brs(24);
	do_html_footer(news);
	exit;
	}
do_html_header("UNY-Con Create csv of attendee's", staysmall);



$filename = 'mailinglist.csv';
//fopen ($filename, 'w+');
if (is_writable($filename)) {
	// open file as writeable and zero it.
	if (!$fp = fopen($filename, 'w+b')) {
		print "<span class=\"redtext\">Cannot open file ($filename)</span>";
		exit;
		}
	//print "Success, wrote ($someconent entries) to file ($filename)";
	fclose($fp);
	} # close iswriteable if
	else {
	echo "<span class=\"redtext\">File is not writeable!</span>";
	}

	$somecontent = '"First Name","Last Name","address","city","state","zip","country","email","date added"';
	$somecontent .= "\n";
	write_to_file($somecontent, $filename);
$conn = db_connect();
$query = "SELECT first_name,last_name,address1 || ' ' || address2 as address,city,state,zip,contact,country,email_addr FROM attendee WHERE address1 != '' AND city != '' AND state != '' AND zip != '' AND contact = 't' ORDER BY last_name ASC";
$result = pg_query("$query");
$values = db_result_to_array($result);
$current_date = date("n\/j\/Y");
foreach ($values as $row) {
	$somecontent = "\"".$row['first_name']."\",\"".$row['last_name']."\",\"".$row['address']."\",\"".$row['city']."\",\"".$row['state']."\",\"".$row['zip']."\",\"".$row['country']."\",\"".$row['email_addr']."\",\"$current_date\"";
	$somecontent .= "\n";
	write_to_file($somecontent, $filename);
	unset($somecontent);
	}

do_html_URL("$filename", "Download $filename");
print_brs(1);
do_html_footer(news);



?>